<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 保障对象统计报表管理
 * Class ReportBlacklistController
 * @package Admin\Controller
 */
class ReportBlacklistController extends BaseController {

    protected function fn_GetLastYears($num=5){
        $years = array();
        $year = intval(date("Y"));
        for($i=-1; $i<$num; $i++){
            $years[] = $year-$i;
        }
        return $years;
    }

    public function ajax_export(){

        header("Content-type: text/html; charset=utf-8");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=黑名单报表.xls");  //文件下载

        $total = 0;
        $blacklist = array();
        $year = I("request.year")?I("request.year"):"all";
        $this->action_GenerateReport($year, $blacklist, $total);

        echo '<table width="200" border="1">
                  <tr>
                    <td>原因</td>
                    <td>数量</td>
                  </tr>';
        foreach($blacklist as $item){
            $tr = "
                <tr>
                <td>{{reason}}</td>
                <td>{{num}}</td>
              </tr>";
            echo  strtr($tr, [
                "{{reason}}" => $item['reason'],
                "{{num}}" => $item['total'],
            ]);
        }
        $footer = "
                <tr>
                <td></td>
                <td>{{total}}</td>
              </tr>
            </table>";
        echo  strtr($footer, ["{{total}}" => $total]);
    }

    private function action_GenerateReport($year, &$blacklist, &$total){
        $where = array();
        if($year!="all"){
            $year1 = $year."/01/01";
            $year2 = $year."/12/31";
            $where["ctime"] = array('between', array($year1, $year2));
        }

        $info = array();
        $reason = M("blacklist_reason")->select();
        foreach($reason as $item){
            $info[$item['name']] = 0;
        }

        $data = M("blacklist")->field("reason, count(id) as total")
            ->where($where)->group("reason")->select();
        foreach ($data as $item) {
            $info[$item['reason']] = $item['total'];
        }

        foreach($info as $key => $value){
            $where['reason'] = $key;
            $blacklist[] = array(
                "reason"=>$key,
                "total"=>$value,
                "where"=>serialize($where)
            );
        }

        foreach ($data as $item) {
            $total += $item['total'];
        }
    }

    /**
     * 保障对象统计报表管理 -> formview
     */
    public function detail(){

        $years = $this->fn_GetLastYears();
        $this->assign("years", $years);
        $year = I("request.year")?I("request.year"):"all";
        $this->assign("year", $year);

        $total = 0;
        $blacklist = array();
        $this->action_GenerateReport($year, $blacklist, $total);
        $this->assign("total", $total);
        $this->assign("blacklist", $blacklist);
        $this->display();
    }
}